<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <title>Course Launch Page</title>
	<script src="scripts/TCDriver.js" type="text/javascript"></script>
	<script src="scripts/common.js" type="text/javascript"></script>
    <script type="text/javascript">
    
		var courseObj = {
            "id":"scorm.com/GolfExample_TCAPI",
            "definition":{
            	"type":"Course",
            	"name":{"en-US":"Golf Example - Tin Can Course"},
            	"description":{"en-US":"An overview of how to play the great game of golf."}
            }
        };

        var stmt = {
            "verb":"attempted",
            "inprogress":true,
            "object":courseObj,
            "context":GolfExample.getContext()
        };


        TCDriver_SendStatement(tc_lrs, stmt);
	
    /*************************************/
    //functions for sizing the iFrame
    /*************************************/
    function setIframeHeight(id, navWidth) {
        if ( document.getElementById ) {
            var theIframe = document.getElementById(id);
            if (theIframe) {
                var height = getWindowHeight();
                theIframe.style.height = Math.round( height ) - navWidth + "px";
                theIframe.style.marginTop = Math.round( ((height - navWidth) - parseInt(theIframe.style.height) )/2 ) + "px";
            }
        }
    }

    function getWindowHeight() {
        var height = 0;
        if (window.innerHeight){
            height = window.innerHeight - 18;
        }
        else if (document.documentElement && document.documentElement.clientHeight){
            height = document.documentElement.clientHeight;
        }
        else if (document.body && document.body.clientHeight) {
            height = document.body.clientHeight;
        }
        return height;
    }
    
    function SetupIFrame(){
        //set our iFrame for the content to take up the full screen except for our navigation
        var navWidth = 80;
        setIframeHeight("contentFrame", navWidth);
        window.onresize = function() { setIframeHeight("contentFrame", navWidth); }
    }
    
    /*************************************/
    //content definition
    /*************************************/
    var pageArray = new Array(16);
    
    pageArray[0] = "Playing/Playing.html";
    pageArray[1] = "Playing/Par.html";
    pageArray[2] = "Playing/Scoring.html";
    pageArray[3] = "Playing/OtherScoring.html";
    pageArray[4] = "Playing/RulesOfGolf.html";
    pageArray[5] = "Etiquette/Course.html";
    pageArray[6] = "Etiquette/Distracting.html";
    pageArray[7] = "Etiquette/Play.html";
    pageArray[8] = "Handicapping/Overview.html";
    pageArray[9] = "Handicapping/CalculatingHandicap.html";
    pageArray[10] = "Handicapping/CalculatingScore.html";
    pageArray[11] = "Handicapping/Example.html";
    pageArray[12] = "HavingFun/HowToHaveFun.html";
    pageArray[13] = "HavingFun/MakeFriends.html";
    pageArray[14] = "HavingFun/HowToBeStylish.html";
    pageArray[15] = "assessmenttemplate.html?questions=Playing&questions=Etiquette&questions=Handicapping&questions=HavingFun";
   
    /*************************************/
    //navigation functions
    /*************************************/
    
    var currentPage = null;
    var startTimeStamp = null;
    var processedUnload = false;
    var reachedEnd = false;
    var maxPageReached = 0;
    
    function doStart(){
        
        //get the iFrame sized correctly and set up
        SetupIFrame();
        		
        //get activity_id bookmark if it exists
       	var bookmark = TCDriver_GetState(tc_lrs, "scorm.com/GolfExample_TCAPI", "location");
        
        //if there isn't a stored bookmark, start the user at the first page
        if (bookmark === undefined || bookmark === null || bookmark == ""){
            currentPage = 0;
        }
        else{
            //if there is a stored bookmark, prompt the user to resume from the previous location
            if (confirm("Would you like to resume from where you previously left off? ")){
                currentPage = parseInt(bookmark, 10);
            }
            else{
                currentPage = 0;
            }
        }
        
        goToPage();
    }
    
    function goToPage(){
    	
        var theIframe = document.getElementById("contentFrame");
        var prevButton = document.getElementById("butPrevious");
        var nextButton = document.getElementById("butNext");

		
        //pass the TC arguments to the iframe
        var tc_argStr = (pageArray[currentPage].indexOf("?") != -1) ? "&" + location.search.slice(1) : location.search;
		
		//navigate the iFrame to the content
        theIframe.src = pageArray[currentPage] + tc_argStr;
        
        //disable the prev/next buttons if we are on the first or last page.
        if (currentPage == 0){
            nextButton.disabled = false;
            prevButton.disabled = true;
        }
        else if (currentPage == (pageArray.length - 1)){
            nextButton.disabled = true;
            prevButton.disabled = false;       
        }
        else{
            nextButton.disabled = false;
            prevButton.disabled = false;
        }

        //save the current location as the bookmark
		TCDriver_SetState(tc_lrs, "scorm.com/GolfExample_TCAPI", "location", currentPage);
        
    }
    
    function doFinish(){
    }
    
    function doPrevious(){
        if (currentPage > 0){
            currentPage--;
        }
        goToPage();
    }
    
    function doNext(){
        if (currentPage < (pageArray.length - 1)){
            currentPage++;
        }
        goToPage();
    }
    
    function setBookmark(){
        TCDriver_SetState (tc_lrs, "scorm.com/GolfExample_TCAPI", "location", currentPage);
    }
    
    //called from the assessmenttemplate.html page to record the results of a test
    //passes in score as a percentage
    function GetRecordTestStatement(score,activityId){
    
		// send score
        var scaledScore = score / 100;
        var success = (scaledScore >= 0.8);

        
        var stmt = { 
            "verb":"completed",
            "object": {"id" : activityId },
            "result": {
                "score":{
                    "scaled":scaledScore,
                    "raw":score,
                    "min":0,
                    "max":100
                },
                "success":success
            },
            "context":GolfExample.getContext(
                        GolfExample.CourseActivity.id)
        };

        return stmt;
    }
    
    
    
    //called from the assessmenttemplate.html page to record the results of a question
    function GetQuestionAnswerStatement(id, questionText, questionType, learnerResponse, correctAnswer, wasCorrect, activityId){
        
		//send question info
 		/*var questionObj = new TCObject().SetValue("id",id);
		var resultObj = new TCObject().SetValue("response",learnerResponse)
				.SetValue("success",wasCorrect);
		var contextObj = new TCObject().SetValue("activity",new TCObject().SetValue("id",activityId));
						
		var stmt = new TCStatement(tc_lrs).SetValue("verb","answered")
			.SetValue("object",questionObj)
			.SetValue("result",resultObj)
			.SetValue("context",contextObj);
		
		return stmt.GetStatement();*/

		var qObj = {
            "id":id,
			"definition":{
                "type":"question",
				"description":{"en-US":questionText},
				"interactionType":questionType,
				"correctResponsesPattern":[String(correctAnswer)]
            }
        };

        return {
            "verb":"answered",
            "object":qObj,
            "result":{
                "response":learnerResponse,
                "success":wasCorrect
            },
            "context":GolfExample.getContext(activityId)
        }
    }
    
    function FormatChoiceResponse(value){
        
        var newValue = new String(value);
        
        //replace all whitespace
        newValue = newValue.replace(/\W/g, "_");
        
        return newValue;
    }
    
    
    
    
    function RoundToPrecision(number, significantDigits){

	    number = parseFloat(number);
        return (Math.round(number * Math.pow(10, significantDigits)) / Math.pow(10, significantDigits))
    }
    
   
    </script>

</head>
<body onload="doStart(false);">
  <div style="text-align:center; width:100%;margin:10px;"><a style="" href="http://www.tincanapi.co.uk">TinCanAPI.co.uk Home</a></div> 
    <iframe width="100%" id="contentFrame" src=""></iframe>
    
    <div id="navDiv">
        <input type="button" value="<- Previous" id="butPrevious" onclick="doPrevious();"/>
        <input type="button" value="Next ->" id="butNext" onclick="doNext();"/>
        <!--<input type="button" value="SetBookmark" id="setBookmark" onclick="setBookmark();"/>-->
    </div>

</body>
</html>
